﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Sql;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HRM.Upgrade.Common;

namespace HRM.Upgrade
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void GetInstance()
        {
            //string myServer = Environment.MachineName;

            DataTable servers = SqlDataSourceEnumerator.Instance.GetDataSources();
            for (int i = 0; i < servers.Rows.Count; i++)
            {
                //if (myServer == servers.Rows[i]["ServerName"].ToString()) ///// used to get the servers in the local machine////
                //{
                //    if ((servers.Rows[i]["InstanceName"] as string) != null)
                //        cmbInstance.Items.Add(servers.Rows[i]["ServerName"] + "\\" + servers.Rows[i]["InstanceName"]);
                //    else
                //        cmbInstance.Items.Add(servers.Rows[i]["ServerName"]);
                //}

                if ((servers.Rows[i]["InstanceName"] as string) != null)
                    cmbInstance.Items.Add(servers.Rows[i]["ServerName"] + "\\" + servers.Rows[i]["InstanceName"]);
                else
                    cmbInstance.Items.Add(servers.Rows[i]["ServerName"]);
            }
        }

        private void btGetInstances_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            cmbInstance.Items.Clear();
            GetInstance();
            if (cmbInstance.Items.Count > 0)
            {
                cmbInstance.SelectedItem = cmbInstance.Items[0];
            }
            Cursor.Current = Cursors.Default;

        }

        private void btCheckDatabase_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            BaseDAO.ConnectionString = "data source=" + cmbInstance.Text + ";User ID=" + txtUsername.Text.Trim() + ";password=" + txtPassword.Text.Trim() + ";initial catalog=master;";
            if (BaseDAO.OpenConnection())
            {
                Utilities.Messages.Notice("Kết nối dữ liệu thành công.");
            }
            else
            {
                Utilities.Messages.Error("Kết nối dữ liệu thất bại. Hãy kiểm tra lại thông tin.");
            }
            Cursor.Current = Cursors.Default;
        }

        private void btExecute_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            string msg = "Start";
            BaseDAO.ConnectionString = "data source=" + cmbInstance.Text + ";User ID=" + txtUsername.Text.Trim() + ";password=" + txtPassword.Text.Trim() + ";initial catalog=master;";

            try
            {
                if (BaseDAO.ExecuteNonQuery(MySqlCommand.Database.DropAndCreate) <= 0) msg += "::MySqlCommand.Database.DropAndCreate";

                if (BaseDAO.ExecuteNonQuery(MySqlCommand.Login.DropAndCreate) <= 0) msg += "::MySqlCommand.Login.DropAndCreate";

                //Tables
                if (BaseDAO.ExecuteNonQuery(MySqlCommand.Tables.ActiveLicense) <= 0) msg += "::MySqlCommand.Tables.ActiveLicense";

                if (BaseDAO.ExecuteNonQuery(MySqlCommand.Tables.Contact) <= 0) msg += "::MySqlCommand.Tables.Contact";

                if (BaseDAO.ExecuteNonQuery(MySqlCommand.Tables.Customer) <= 0) msg += "::MySqlCommand.Tables.Customer";

                if (BaseDAO.ExecuteNonQuery(MySqlCommand.Tables.KeyCode) <= 0) msg += "::MySqlCommand.Tables.KeyCode";

                if (BaseDAO.ExecuteNonQuery(MySqlCommand.Tables.License) <= 0) msg += "::MySqlCommand.Tables.License";

                if (BaseDAO.ExecuteNonQuery(MySqlCommand.Tables.Soft) <= 0) msg += "::MySqlCommand.Tables.Soft";

                if (BaseDAO.ExecuteNonQuery(MySqlCommand.Tables.SoftStyle) <= 0) msg += "::MySqlCommand.Tables.SoftStyle";

                //Stored
                BaseDAO.ConnectionString = "data source=" + cmbInstance.Text + ";User ID=" + txtUsername.Text.Trim() + ";password=" + txtPassword.Text.Trim() + ";initial catalog=Perfectmd;";

                if (BaseDAO.ExecuteNonQuery(MySqlCommand.StoredProcedure.Customer_GetById) <= 0) msg += "::MySqlCommand.StoredProcedure.Customer_GetById";

                if (BaseDAO.ExecuteNonQuery(MySqlCommand.StoredProcedure.Customer_Insert) <= 0) msg += "::MySqlCommand.StoredProcedure.Customer_Insert";

                if (BaseDAO.ExecuteNonQuery(MySqlCommand.StoredProcedure.Keycode_GetByKeyCode) <= 0) msg += "::MySqlCommand.StoredProcedure.Keycode_GetByKeyCode";

                if (BaseDAO.ExecuteNonQuery(MySqlCommand.StoredProcedure.Keycode_Insert) <= 0) msg += "::MySqlCommand.StoredProcedure.Keycode_Insert";

                if (BaseDAO.ExecuteNonQuery(MySqlCommand.StoredProcedure.Keycode_Update) <= 0) msg += "::MySqlCommand.StoredProcedure.Keycode_Update";

                if (BaseDAO.ExecuteNonQuery(MySqlCommand.StoredProcedure.License_GetByKeyCode) <= 0) msg += "::MySqlCommand.StoredProcedure.License_GetByKeyCode";

                if (BaseDAO.ExecuteNonQuery(MySqlCommand.StoredProcedure.License_GetMachKeyCode) <= 0) msg += "::MySqlCommand.StoredProcedure.License_GetMachKeyCode";

                if (BaseDAO.ExecuteNonQuery(MySqlCommand.StoredProcedure.License_Insert) <= 0) msg += "::MySqlCommand.StoredProcedure.License_Insert";

                if (BaseDAO.ExecuteNonQuery(MySqlCommand.StoredProcedure.License_InsertName) <= 0) msg += "::MySqlCommand.StoredProcedure.License_InsertName";

                if (BaseDAO.ExecuteNonQuery(MySqlCommand.StoredProcedure.License_Update) <= 0) msg += "::MySqlCommand.StoredProcedure.License_Update";

                if (BaseDAO.ExecuteNonQuery(MySqlCommand.StoredProcedure.License_UpdateReActive) <= 0) msg += "::MySqlCommand.StoredProcedure.License_UpdateReActive";

                Execute exe = new Execute(BaseDAO.ConnectionString);
                if(exe.CustomerInsert(txtCompanyName.Text.Trim(), txtAddress.Text.Trim(), txtTell.Text.Trim(), txtMobile.Text.Trim(), txtFax.Text.Trim(), txtWebsite.Text.Trim(), txtEmail.Text.Trim(),
                    txtTaxId.Text.Trim(), txtBusinessLicense.Text.Trim(), txtContactMan.Text.Trim(), txtPosition.Text.Trim()) <=0)
                    msg += "::Execute.CustomerInsert";
                

                int idCust = (int)BaseDAO.ExecuteScalar("select max(Id_Customer) from [CUSTOMER]");
                if (exe.KeyCodeInsert(Constants.KeyCode, 6, idCust, 100, 0) <=0)
                    msg += "::Execute.KeyCodeInsert";


                Cursor.Current = Cursors.Default;

                Utilities.Messages.Notice(msg + "::OK");
            }
            catch (Exception)
            {
                Utilities.Messages.Notice(msg);
            }
        }
    }
}
